package com.liam.chinesevietnamesecorpus.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.liam.chinesevietnamesecorpus.entity.CorpusLibrary;
import com.liam.chinesevietnamesecorpus.vo.CorpusLibraryStatisticsVO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

/**
 * 语料库Mapper接口
 * 
 * @author Liam
 */
@Mapper
public interface CorpusLibraryMapper extends BaseMapper<CorpusLibrary> {

    /**
     * 根据ID查询语料库统计信息
     * 
     * @param libraryId 语料库ID
     * @return 统计信息
     */
    @Select("SELECT " +
            "cl.id, " +
            "cl.name, " +
            "cl.description, " +
            "cl.domain, " +
            "COUNT(ci.id) as totalItems, " +
            "COUNT(CASE WHEN ci.status = 'active' THEN 1 END) as activeItems, " +
            "COUNT(CASE WHEN ci.status = 'inactive' THEN 1 END) as inactiveItems, " +
            "MIN(ci.create_time) as firstItemTime, " +
            "MAX(ci.create_time) as lastItemTime " +
            "FROM corpus_library cl " +
            "LEFT JOIN corpus_item ci ON cl.id = ci.library_id " +
            "WHERE cl.id = #{libraryId} AND cl.status != 'deleted' " +
            "GROUP BY cl.id, cl.name, cl.description, cl.domain")
    CorpusLibraryStatisticsVO getLibraryStatistics(Long libraryId);
}
